package cn.edu.neusoft.yuejian12.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;
import java.util.List;

import cn.edu.neusoft.yuejian12.Adapter.HactivityAdapter;
import cn.edu.neusoft.yuejian12.Adapter.TrainListAdapter;
import cn.edu.neusoft.yuejian12.JavaBean.Activity;
import cn.edu.neusoft.yuejian12.JavaBean.TrainInfo;
import cn.edu.neusoft.yuejian12.Model.ActivityModel;
import cn.edu.neusoft.yuejian12.Model.UserModel;
import cn.edu.neusoft.yuejian12.R;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 木子李 on 2017/10/16.
 */

public class HactivityFragment extends Fragment {
    private RecyclerView recyclerView;
    private String userid;
    private Intent intent;


    private HactivityAdapter hactivityAdapter;

    private ImageView btn_collect;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getUserId();
        recyclerView = (RecyclerView)view.findViewById(R.id.rv_hactivity);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.h_activity, null);
//
//        recyclerView = (RecyclerView)view.findViewById(R.id.rv_hactivity);
//        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
//
//
//        getUserId();

        return view;
    }

    private void getUserId(){
        intent = getActivity().getIntent();
        userid = intent.getStringExtra("uid");
    }

    @Override
    public void onResume() {
        super.onResume();
//        getUserId();
        getHactivity();
    }


    private void getHactivity(){
     int uid = Integer.parseInt(userid);

        ActivityModel activityModel = new ActivityModel();
        Call<List<Activity>> call = activityModel.getAllActivity();
        call.enqueue(new Callback<List<Activity>>() {
            @Override
            public void onResponse(Call<List<Activity>> call, Response<List<Activity>> response) {
                List<Activity> mDataList = response.body();
                recyclerView.setAdapter(new HactivityAdapter(HactivityFragment.this.getContext(),mDataList));//设置适配器，绑定Adapter.


            }

            @Override
            public void onFailure(Call<List<Activity>> call, Throwable t) {
                Toast.makeText(getActivity(),"fail",Toast.LENGTH_SHORT).show();
            }
        });
    }

}
